using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Cpsc481Phone.Gui.Menus
{
    public partial class Messaging : UserControl
    {
        private Boolean optionsUp = false;

        private String[] abc = {"a", "b", "c"};
        private String[] def = { "d", "e", "f" };
        private String[] ghi = { "g", "g", "i" };
        private String[] jkl = { "j", "k", "l" };
        private String[] mno = { "m", "n", "o" };
        private String[] pqrs = { "p", "q", "r" , "s"};
        private String[] tuv = { "t", "u", "v" };
        private String[] wxyz = { "w", "x", "y", "z" };

        private int currentLetter = -1;

        private int counter = 0;
        private int letterToSet = 0;
        private String oldText;
        
  
        public Messaging()
        {
            InitializeComponent();
            listBox1.Items.Add("Send");
            listBox1.Items.Add("Save");            
            listBox1.Visible = false;
            richTextBox1.Text = " ";
        }
        public void optionsButtonPress()
        {
            listBox1.Visible = true;
            listBox1.SelectedIndex = 0;
        }
        public Boolean getOptionsMenuUp()
        {
            return optionsUp;
        }
        public void cursorUp()
        {
            if(listBox1.SelectedIndex<listBox1.Items.Count)
                listBox1.SelectedIndex++;            
        }
        public void cursorDown()
        {
            if (listBox1.SelectedIndex < 0)
                listBox1.SelectedIndex--;
        }

        public Boolean shouldLetterChange()
        {
            if (counter < 2)
                return true;
            else
            {
                counter = 0;
                currentLetter = -1;                              
                return false;
            }
        }
        public void clickABC()
        {
            // need to fix the -1 bug it should print the letter when its -1 also. 
            counter = 0;
            if( currentLetter == - 6)
            {
                currentLetter = 2;                
                oldText = richTextBox1.Text;
            }
            else if (currentLetter == 2)
            {
                // the button pressed is the same as last time. 
                if (letterToSet < abc.Length - 1)
                    letterToSet++;
                else
                    letterToSet = 0;
                int cusorPos = this.richTextBox1.SelectionStart;
                richTextBox1.Text = oldText;
                this.richTextBox1.SelectionStart = cusorPos;
                
                Clipboard.SetText(abc[letterToSet], TextDataFormat.Rtf);
                richTextBox1.Paste();
                this.richTextBox1.SelectionStart++;
            }
            else
            {
                // the last button was not this and it there was no idle cycle between.
                currentLetter = 2;
                letterToSet = 0;
                oldText = richTextBox1.Text;
                Clipboard.SetText(abc[0], TextDataFormat.Rtf);
                richTextBox1.Paste();
                this.richTextBox1.SelectionStart++;
            }
            counter = 0;
        }

        public void clickDEF()
        {
            counter = 0;
            if (currentLetter == -6)
            {
                currentLetter = 3;
                oldText = richTextBox1.Text;
            }
            else if (currentLetter == 3)
            {
                // the button pressed is the same as last time. 
                if (letterToSet < def.Length - 1)
                    letterToSet++;
                else
                    letterToSet = 0;
                int cusorPos = this.richTextBox1.SelectionStart;
                richTextBox1.Text = oldText;
                this.richTextBox1.SelectionStart = cusorPos;
                Clipboard.SetText(def[letterToSet], TextDataFormat.Rtf);
                richTextBox1.Paste(); 
                this.richTextBox1.SelectionStart++;
            }
            else
            {
                // the last button was not this and it there was no idle cycle between.
                currentLetter = 3;
                letterToSet = 0;
                oldText = richTextBox1.Text;
                Clipboard.SetText(def[0], TextDataFormat.Rtf);
                richTextBox1.Paste(); 
                this.richTextBox1.SelectionStart++;
            }
            counter = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            shouldLetterChange();
        }
        


        




    }
}
